package com.sc.servlet.user;

import com.sc.pojo.User;
import com.sc.services.UserServices;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description: 用户注册
 * @author: sc
 * @create: 2023-01-06 17:56
 **/
@WebServlet("/registerUser")
public class registerUserServlet extends HttpServlet {
    private UserServices userServices = new UserServices();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset = utf-8");
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        User user = new User();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("checkCode");
        String phone = request.getParameter("phone");
        String sex = request.getParameter("sex");
        String userid = request.getParameter("userid");

        user.setUserid(userid);
        user.setUsername(username);
        user.setPhone(phone);
        user.setSex(sex);
        user.setPassword(password);


        String checkcode = (String) session.getAttribute("checkCodeGen");

        boolean userExist = userServices.isUserExist(user.getUserid());


        if(!userExist){
                userServices.addUser(user);
                request.setAttribute("suc_mes","注册成功请登录!");
                request.getRequestDispatcher("login.jsp").forward(request,response);
            }
            else{
                request.setAttribute("userid_err","用户id已存在 ！");
                request.getRequestDispatcher("register.jsp").forward(request,response);
            }

        }



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }


}
